package com.elvin.platform.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.elvin.platform.beans.Organize;
import com.elvin.platform.service.OrganizeService;
import com.elvin.platform.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RestController
@RequestMapping("/organizeController")
@Log4j2
@Api("组织管理")
public class OrganizeController {

  @Resource
  private OrganizeService service;

  @ApiOperation(value = "删除组织")
  @PostMapping("/delete")
  public Result<Object> delete(@ApiParam(name = "id", value = "组织id", required = true) @RequestParam("id") Long id) {
    Result<Object> result = new Result<>();
    try {
      service.deleteById(id);
      result.success("删除成功");
    } catch (Exception e) {
      log.debug(e.getMessage());
      result.fail(null, e.getMessage());
    }
    return result;
  }

  @ApiOperation(value = "根据组织机构id删除当前机构和子机构")
  @PostMapping("/deleteSelfAndChild")
  public Result<Object> deleteSelfAndChild(@ApiParam(name = "id", value = "组织id", required = true) @RequestParam("id") Long id) {
    Result<Object> result = new Result<>();
    try {
      service.deleteSelfAndChild(id);
      result.success("删除成功");
    } catch (Exception e) {
      log.debug(e.getMessage());
      result.fail(null, e.getMessage());
    }
    return result;
  }

  @ApiOperation(value = "根据ID获取组织")
  @PostMapping("/getById")
  public Result<Object> getById(@ApiParam(name = "id", value = "组织id", required = true) @RequestParam("id") Long id) {
    Result<Object> result = new Result<>();
    try {
      Organize entity = new Organize();
      entity.setId(id);
      entity = service.selectById(entity);
      result.success(entity);
    } catch (Exception e) {
      log.debug(e.getMessage());
      result.fail(null, e.getMessage());
    }
    return result;
  }

  @ApiOperation(value = "新增组织")
  @PostMapping("/insert")
  public Result<Object> insert(@RequestBody Organize entity) {
    Result<Object> result = new Result<>();
    try {
      entity.setCreateTime(new Date());
      service.insert(entity);
      result.success(entity, "新增成功");
    } catch (Exception e) {
      log.debug(e.getMessage());
      result.fail(null, e.getMessage());
    }
    return result;
  }

  @ApiOperation(value = "新增组织")
  @PostMapping("/insertOrganization")
  public Result<Object> insertOrganization(@RequestBody Organize entity) {
    Result<Object> result = new Result<>();
    try {
      entity.setCreateTime(new Date());
      service.insertOrganization(entity);
      result.success(entity, "新增成功");
    } catch (Exception e) {
      log.debug(e.getMessage());
      result.fail(null, e.getMessage());
    }
    return result;
  }

  @ApiIgnore
  @ApiOperation(value = "获取所有组织")
  @PostMapping("/queryAll")
  public Result<Object> queryAll() {
    Result<Object> result = new Result<>();
    try {
      List<Organize> selectList = service.selectList(null);
      result.success(selectList);
    } catch (Exception e) {
      log.debug(e.getMessage());
      result.fail(null, e.getMessage());
    }
    return result;
  }

  @ApiOperation(value = "分页获取所有数据")
  @PostMapping("/selectPage")
  public Result<Object> selectForPage(
      @ApiParam("当前页码") @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
      @ApiParam("当前页显示条数") @RequestParam(value = "pageSize", required = false) Integer pageSize
  ) {
    Result<Object> result = new Result<>();
    try {
      int count = service.selectCount(null);
      if (count > 0) {
        List<Organize> selectList = new ArrayList<>();
        if (pageNumber != null && pageSize != null) {
          Page<Organize> page = new Page<>(pageNumber, pageSize);
          service.selectPage(page, null);
          selectList = page.getRecords();
        } else {
          selectList = service.selectList(null);
        }
        result.success(selectList, count);
      } else {
        result.success(null, count);
      }

    } catch (Exception e) {
      log.debug(e.getMessage());
      result.fail(null, e.getMessage());
    }
    return result;
  }

  @ApiOperation(value = "查询当前组织和下级组织")
  @PostMapping("/selectSelfAndChild")
  public Result<Object> selectSelfAndChild(@ApiParam(name = "id", value = "组织id", required = true) @RequestParam("id") Long id
  ) {
    Result<Object> result = new Result<>();
    try {
      if (!StringUtils.isEmpty(id)) {
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("id", id);
        List<Organize> selectList = service.selectSelfAndChild(hashMap);
        result.success(selectList);
      } else {
        result.success(null);
      }
    } catch (Exception e) {
      log.debug(e.getMessage());
      result.fail(null, e.getMessage());
    }
    return result;
  }

  @ApiOperation(value = "修改组织")
  @PostMapping("/update")
  public Result<Object> update(@RequestBody Organize entity) {
    Result<Object> result = new Result<>();
    try {
      service.updateById(entity);
      result.success("修改成功");
    } catch (Exception e) {
      log.debug(e.getMessage());
      result.fail(null, e.getMessage());
    }
    return result;
  }


}
